<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="品牌名称">
        <el-input v-model="brandName" class="bdwidth"></el-input>
      </el-form-item>
      <!-- 上传 -->
      <el-form-item label="品牌图片">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :before-upload="beforeAvatarUploadz"
          :http-request="uploadGoodPicz"
        >
          <img v-if="imageUrl1" :src="imageUrl1" class="avatar" />
          <i
            v-else
            class="el-icon-plus avatar-uploader-icon pictureLocation"
          ></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="品牌列表图片">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :before-upload="beforeAvatarUploadx"
          :http-request="uploadGoodPicx"
        >
          <img v-if="imageUrl2" :src="imageUrl2" class="avatar" />
          <i
            v-else
            class="el-icon-plus avatar-uploader-icon pictureLocation"
          ></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="app专用图片">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :before-upload="beforeAvatarUploadd"
          :http-request="uploadGoodPicd"
        >
          <img v-if="imageUrl3" :src="imageUrl3" class="avatar" />
          <i
            v-else
            class="el-icon-plus avatar-uploader-icon pictureLocation"
          ></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="最低价">
        <el-input
          v-model="minimumPrice"
          class="bdwidth"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="新品">
        <el-switch v-model="delivery1"></el-switch>
      </el-form-item>
      <el-form-item label="显示">
        <el-switch v-model="delivery2"></el-switch>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="reorder" class="bdwidth"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="adddialogVisible = true"
          >确认编辑</el-button
        >
        <el-button @click="offSubmit">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 确认框 -->
    <el-dialog title="提示" :visible.sync="adddialogVisible" width="50%">
      <span>你确定这样编辑吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  UploadAddbrandNewPicApi, //上传图片的
  UploadAddbrandPicApi, //品牌添加
} from "@/request/httpApi.js";
export default {
  data() {
    return {
      adddialogVisible: false, //确认框状态
      // 开关
      delivery1: false, //新品
      delivery2: false, //显示
      id: "", //id
      //   品牌名称
      brandName: "",
      //   最低价
      minimumPrice: "",
      //   新品
      // 显示
      //   排序
      reorder: "",
      imageUrl1: "", //品牌图片地址
      imageUrl2: "", //品牌列表图片地址
      imageUrl3: "", //app专用图片地址
      headers: { "X-Nideshop-Token": localStorage.getItem("token") }, //请求头
    };
  },
  methods: {
    //   覆盖默认
    uploadGoodPicz(data) {
      let formdata = new FormData();
      formdata.append("good_pic", data.file);
      UploadAddbrandNewPicApi(formdata).then((res) => {
        if (res.errno === 0) {
          this.imageUrl1 = res.data.fileUrl;
        }
      });
    },
    uploadGoodPicx(data) {
      let formdata = new FormData();
      formdata.append("good_pic", data.file);
      UploadAddbrandNewPicApi(formdata).then((res) => {
        if (res.errno === 0) {
          this.imageUrl2 = res.data.fileUrl;
        }
      });
    },
    uploadGoodPicd(data) {
      let formdata = new FormData();
      formdata.append("good_pic", data.file);
      UploadAddbrandNewPicApi(formdata).then((res) => {
        if (res.errno === 0) {
          this.imageUrl3 = res.data.fileUrl;
        }
      });
    },
    // 上传图片
    handleAvatarSuccessz(res, file) {
      if (res.errno === 0) {
        this.imageUrl1 = res.data.fileUrl;
      }
      console.log(res);
      console.log(file);
    },
    handleAvatarSuccessx(res, file) {
      if (res.errno === 0) {
        this.imageUrl2 = res.data.fileUrl;
      }
      console.log(res);
      console.log(file);
    },
    handleAvatarSuccessd(res, file) {
      if (res.errno === 0) {
        this.imageUrl3 = res.data.fileUrl;
      }
      console.log(res);
      console.log(file);
    },
    beforeAvatarUploadz(file) {},
    beforeAvatarUploadx(file) {},
    beforeAvatarUploadd(file) {},

    //
    // 确定编辑
    onSubmit() {
      // 转换默认值
      if (this.delivery1) {
        this.delivery1 = 1;
      } else {
        this.delivery1 = 0;
      }
      if (this.delivery2) {
        this.delivery2 = 1;
      } else {
        this.delivery2 = 0;
      }
      // 编辑
      UploadAddbrandPicApi({
        id: this.id, //id
        name: this.brandName, //品牌名称
        list_pic_url: this.imageUrl1, //品牌图片
        pic_url: this.imageUrl2, //品牌列表图片
        sort_order: this.reorder, //排序
        is_show: this.delivery2, //显示
        floor_price: this.minimumPrice, //最低价
        app_list_pic_url: this.imageUrl3, //app专用图片
        is_new: this.delivery1, //新品
      }).then((res) => {
        if (res.errno === 0) {
          this.id = res.data.id;
          this.brandName = res.data.name;
          this.imageUrl1 = res.data.list_pic_url;
          this.imageUrl2 = res.data.pic_url;
          this.reorder = res.data.sort_order;
          this.delivery2 = res.data.is_show;
          this.minimumPrice = res.data.floor_price;
          this.imageUrl3 = res.data.app_list_pic_url;
          this.delivery1 = res.data.is_new;

          this.$router.push("/brand");

          this.adddialogVisible = false;
        }
      });
    },

    // 取消编辑
    offSubmit() {
      this.$router.push("/brand");
    },
  },
  created() {
    this.id = this.$route.query.id;

    UploadAddbrandPicApi({
      id: this.id, //id
    }).then((res) => {
      if (res.errno === 0) {
        this.id = res.data.id;
        this.brandName = res.data.name;
        this.imageUrl1 = res.data.list_pic_url;
        this.imageUrl2 = res.data.pic_url;
        this.reorder = res.data.sort_order;
        this.delivery2 = res.data.is_show;
        this.minimumPrice = res.data.floor_price;
        this.imageUrl3 = res.data.app_list_pic_url;
        this.delivery1 = res.data.is_new;
      }
    });
    // 初始化状态
    this.adddialogVisible = false;
  },
};
</script>
 
<style lang = "less" scoped>
.pictureLocation {
  width: 187px;
  height: 105px;
  line-height: 105px;
  border: 1px solid #ccc;
}
.bdwidth {
  width: 250px;
}
</style>